package com.foreveross.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.foreveross.common.Constants;


public class StringUtil {

	/**
	 * 截取文本内容里面的图片地址
	 * @param html
	 * @return
	 */
	public static List<String> getImgSrc(String html) {
		 List<String> list = new ArrayList<String>();
		 Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",
					Pattern.CASE_INSENSITIVE| Pattern.MULTILINE);
		 Matcher m = p.matcher(html);
		 while(m.find()){
	        	String url = m.group(1);
	        	if(url.indexOf(Constants.WEBAPP_PATH) != -1){
	        		url = url.substring(url.indexOf(Constants.WEBAPP_PATH)+Constants.WEBAPP_PATH.length()-1,
	        				url.length());
	        	}
	        	list.add(url);
	        }
		return list;
	}
	
	public static <T extends Object> T MapToClazz(Map map,Class<T> clazz){
		T t = null;
		try{
			t = clazz.newInstance();
			Field[] fileds = clazz.getDeclaredFields();
			
			 //写数据
			for(Field field : fileds) {
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
				//获得写方法
				Method method = pd.getWriteMethod();
				Class fieldType = field.getType();
				String value = (String)map.get(field.getName());
				if(value != null && !"".equals(value) && fieldType.equals(int.class)){
					method.invoke(t, Integer.parseInt(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(long.class)){
					method.invoke(t, Long.parseLong(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(float.class)){
					method.invoke(t, Float.parseFloat(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(Boolean.class)){
					method.invoke(t, Boolean.parseBoolean(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(double.class)){
					method.invoke(t, Double.parseDouble(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(String.class)){
					method.invoke(t, (String)map.get(field.getName()));
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return t;
	}
	
	/**
	 * 更新对象值
	 * @param oldT 被更新的对象
	 * @param newT 改变的值的对象
	 * @return
	 */
	public static <T> T setNewToOldObject(T oldT,T newT){
		try{
			Class<T> oldClass = (Class<T>) oldT.getClass();
			Class<T> newClass = (Class<T>) newT.getClass();
			
			Field[] fileds = oldClass.getDeclaredFields();
			
			 //写数据
			for(Field field : fileds) {
				PropertyDescriptor oldProperty = new PropertyDescriptor(field.getName(), oldClass);
				PropertyDescriptor newProperty = new PropertyDescriptor(field.getName(), newClass);
				
				Method readMethod = newProperty.getReadMethod();
				Object value = readMethod.invoke(newT);
				if(null!=value && !"".equals(value)){
					Method writeMethod = oldProperty.getWriteMethod();
					writeMethod.invoke(oldT, value);
				}
			
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return oldT;
	}
}
